/*
 * Created by Wildish on 2016-10-28
 *
 * Contact me with keithknight@qq.com
 *
 * Copyright (c) 2016 Wildish
 *
 */

package com.wildish.qcface.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;

import com.qcloud.CloudClient;
import com.qcloud.PicCloud;
import com.qcloud.fr.QcloudFrSDK;
import com.qcloud.fr.sign.QcloudFrSign;

import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *
 * Created by Wildish on 2016-10-26.
 */
public class Global {
    public static String cardId;
    public static String name;
    private static File tempFolder;

    private static SharedPreferences sp;
    public static final String SP_INFO = "sp_info";
    public static final String SP_CARD = "sp_card";
    public static final String SP_NAME = "sp_name";

    public static final ExecutorService pool = Executors.newFixedThreadPool(5);

    // 优图相关配置项
    private static final int APP_ID_V2 = 10070395;
    public static final String APP_ID_V2STR = "10070395";
    public static final String SECRET_ID_V2 = "AKIDwpbEfuHbLLo6IOy5at0sSBNTO6cq31LS";
    public static final String SECRET_KEY_V2 = "60ihS8g598zmTHvIckFbo16zui4JOHEB";
    public static final String BUCKET = "caiwu";
    public static final int EXPIRED_SECONDS = 2592000;  // 30 days
    public static final String GROUP_NAME = "default";

    private static QcloudFrSDK qcSDK;

    private static PicCloud picCloud;
    private static CloudClient cloudClient;

    public static final String URI_IDCARDCOMPARE = "http://service.image.myqcloud.com/face/idcardcompare";
    public static int ERROR_PERSON_EXISTED = -1302;
    public static int ERROR_PERSON_NOT_EXISTED = -1303;

    public static void initStoredInfo(Context context) {
        if(sp == null) {
            sp = context.getSharedPreferences(SP_INFO, Context.MODE_PRIVATE);
        }
        cardId = sp.getString(SP_CARD, "");
        name = sp.getString(SP_NAME,"");
    }


    public static QcloudFrSDK getQcSDK() {
        if(qcSDK == null) {
            qcSDK = initQcSDK();
        }
        return qcSDK;
    }

    public static PicCloud getPicCloud() {
        if(picCloud == null) {
            picCloud = new PicCloud(APP_ID_V2, SECRET_ID_V2, SECRET_KEY_V2, BUCKET);
        }
        return picCloud;
    }

    public static CloudClient getCloudClient() {
        if(cloudClient == null) {
            cloudClient = new CloudClient();
        }
        return cloudClient;
    }

    public static File getTempFolder() {
        if(tempFolder == null) {
            tempFolder = new File(Environment.getExternalStorageDirectory(), "com.wildish.qcface");
            if(!tempFolder.exists()) {
                tempFolder.mkdirs();
            }
        }
        return tempFolder;
    }

    public static void setCardId(String cardId) {
        if(sp != null) {
            sp.edit().putString(SP_CARD, cardId).apply();
        }
        Global.cardId = cardId;
    }

    public static void setName(String name) {
        if(sp != null) {
            sp.edit().putString(SP_NAME, name).apply();
        }
        Global.name = name;
    }
    private static QcloudFrSDK initQcSDK() {
        StringBuffer mySign = new StringBuffer("");
        QcloudFrSign.appSign(APP_ID_V2STR, SECRET_ID_V2, SECRET_KEY_V2, System.currentTimeMillis() / 1000 + EXPIRED_SECONDS, "", mySign);

        return new QcloudFrSDK(APP_ID_V2STR, mySign.toString());
    }

    public static String generateTempPhotoPath() {
        return Global.getTempFolder().getAbsolutePath() + File.separator + System.currentTimeMillis() + ".jpg";
    }
}
